package com.haowu.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haowu.common.core.utils.StringUtils;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.haowu.product.domain.HwServiceSku;
import com.haowu.product.domain.bo.HwServiceSkuBo;
import com.haowu.product.domain.vo.HwServiceSkuVo;
import com.haowu.product.mapper.HwAppServiceSkuMapper;
import com.haowu.product.service.IHwAppServiceSkuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 服务项目skuService业务层处理
 *
 * @author lky
 * @date 2023-02-24
 */
@RequiredArgsConstructor
@Service
public class HwAppServiceSkuServiceImpl implements IHwAppServiceSkuService {

    private final HwAppServiceSkuMapper baseMapper;

    /**
     * 查询服务项目sku
     */
    @Override
    public HwServiceSkuVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务项目sku列表
     */
    @Override
    public TableDataInfo<HwServiceSkuVo> queryPageList(HwServiceSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HwServiceSku> lqw = buildQueryWrapper(bo);
        Page<HwServiceSkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务项目sku列表
     */
    @Override
    public List<HwServiceSkuVo> queryList(HwServiceSkuBo bo) {
        LambdaQueryWrapper<HwServiceSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HwServiceSku> buildQueryWrapper(HwServiceSkuBo bo) {
        LambdaQueryWrapper<HwServiceSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getServiceId() != null, HwServiceSku::getServiceId, bo.getServiceId());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceSkuCode()), HwServiceSku::getServiceSkuCode, bo.getServiceSkuCode());
        lqw.like(StringUtils.isNotBlank(bo.getServiceSkuName()), HwServiceSku::getServiceSkuName, bo.getServiceSkuName());
        lqw.eq(bo.getServiceNumber() != null, HwServiceSku::getServiceNumber, bo.getServiceNumber());
        lqw.eq(bo.getOriginalCostAmount() != null, HwServiceSku::getOriginalCostAmount, bo.getOriginalCostAmount());
        lqw.eq(bo.getCurrentPriceAmount() != null, HwServiceSku::getCurrentPriceAmount, bo.getCurrentPriceAmount());
        lqw.eq(bo.getDistributionAmount() != null, HwServiceSku::getDistributionAmount, bo.getDistributionAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverPicture()), HwServiceSku::getCoverPicture, bo.getCoverPicture());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceStatus()), HwServiceSku::getServiceStatus, bo.getServiceStatus());
        lqw.eq(bo.getIsExperience() != null, HwServiceSku::getIsExperience, bo.getIsExperience());
        lqw.eq(bo.getIsDelete() != null, HwServiceSku::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getSort() != null, HwServiceSku::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增服务项目sku
     */
    @Override
    public Boolean insertByBo(HwServiceSkuBo bo) {
        HwServiceSku add = BeanUtil.toBean(bo, HwServiceSku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务项目sku
     */
    @Override
    public Boolean updateByBo(HwServiceSkuBo bo) {
        HwServiceSku update = BeanUtil.toBean(bo, HwServiceSku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HwServiceSku entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除服务项目sku
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
